package com.pz.gulimall.auth.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson2.JSON;
import com.pz.gulimall.auth.service.UserService;
import com.pz.gulimall.auth.vo.User;
import com.pz.gulimall.common.to.UserInfoTo;
import com.pz.gulimall.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("user")
public class UserInfoController {
    @Autowired
    private UserService userService;

    /**
     * 获取登录 session
     * @return
     */
    @GetMapping("/get-loginInfo")
    public R getLoginInfo(){
        return R.ok().put("data", JSON.toJSON(StpUtil.getSession().get("user")));
    }

    @GetMapping("/get-userInfo")
    public R getUserInfo(){
        return userService.getUserInfo(getUserId());
    }

    @PostMapping("/updateInfo")
    public R updateInfo(@RequestBody UserInfoTo userInfoTo){
        return userService.updateUserInfo(userInfoTo);
    }

    /**
     * 删除收货地址
     * @param ids 收货地址ID
     */
    @PostMapping("/delAddress")
    public R delAddress(@RequestBody Long[] ids){
        return userService.delAddress(ids);
    }

    /**
     * 添加收货地址
     */
    @PostMapping("/add-address")
    public R addAddress(@RequestBody Map<String, Object> address){
        return userService.addAddress(getUserId(), address);
    }

    private long getUserId(){
        return JSON.to(User.class, StpUtil.getSession().get("user")).getId();
    }
}

